﻿   
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions; 

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.Dto.MQXT; 
using Oliving.Edge.IRepository.MQXT;
using Oliving.Edge.Model.Models.MQXT;

namespace Oliving.Edge.Repository.MQXT
{
	/// <summary>
	/// MQXT_CaiJiQiModelRepository
	/// </summary>
    public class MQXT_CaiJiQiModelRepository : BaseRepository<MQXT_CaiJiQiModel>, IMQXT_CaiJiQiModelRepository
    {
        public MQXT_CaiJiQiModelRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
   





             /// <summary>
             /// 自定义分页查询
             /// </summary>
             /// <param name="whereExpression">查询条件</param>
             /// <param name="pageIndex" > 页码（下标0）</param>
             /// <param name="pageSize" > 页大小</param>
             /// <param name="sortField" > 排序字段</param>
             /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
             /// <returns></returns>
            public async Task<PageModel<MQXT_CaiJiQiModelPageDto>> QueryPageList(
                    Expression<Func<MQXT_CaiJiQiModel, bool>> whereExpression,
                    int pageIndex = 1, int pageSize = 20,
                    string sortField = null, string sortOrder = null
                    )
            {


                #region 排序

                string strOrderByFileds = null;

                if (!string.IsNullOrEmpty(sortField))
                {
                    strOrderByFileds = sortField;
                    if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                    {
                        strOrderByFileds = strOrderByFileds + " desc";
                    }
                    else
                    {
                        strOrderByFileds = strOrderByFileds + " asc";
                    }
                }

                #endregion

                if (1 == 1)
                {
                    #region 单表

                    RefAsync<int> totalCount = 0;
                    var list = await Db.Queryable<MQXT_CaiJiQiModel>()
                             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                             .WhereIF(whereExpression != null, whereExpression)
                             .Select<MQXT_CaiJiQiModelPageDto>()
                             .ToPageListAsync(pageIndex, pageSize, totalCount);

                    int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
                    return new PageModel<MQXT_CaiJiQiModelPageDto>()
                    {
                        dataCount = totalCount,
                        pageCount = pageCount,
                        page = pageIndex,
                        PageSize = pageSize,
                        data = list
                    };

                    #endregion
                }

                {
                    //var list = Db.Queryable<DTSN_Canning, DTSN_TongDao>
                    //   ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })
                    // //.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                    // .WhereIF(whereExpression != null, whereExpression)
                    // .GroupBy((c, td) => td.TD_Type)
                    // .GroupBy((c, td) => Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString())
                    //   .Select((c, td) => new
                    //   {
                    //       date = Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString(),
                    //       type = td.TD_Type,
                    //       sum = SqlFunc.AggregateSum(c.Canning_NetWeight)
                    //   }).ToList();
                }


            }



 
           
            

    }
}
                 
